package com.banciyuan.bcywebview.biz.detail.mixweb.bridge;

import android.content.Context;
import android.webkit.JavascriptInterface;

import com.banciyuan.bcywebview.R;
import com.banciyuan.bcywebview.base.util.BaseObserver;
import com.banciyuan.bcywebview.utils.common.UIUtils;
import com.banciyuan.bcywebview.utils.string.StringUtil;
import com.google.gson.JsonObject;

import java.util.List;

import de.greenrobot.daoexample.model.Complex;
import de.greenrobot.daoexample.model.UserDetail;

/**
 * Created by squall on 2016/11/23.
 */

public class ReaderBridge {

    private int height;
    private Context mContext;
    private OnLoadingComplate onLoadingComplate;
    private Complex complex;

    public void setComplex(Complex complex) {
        this.complex = complex;
    }

    public ReaderBridge(Context c) {
        this.mContext = c;
    }

    public ReaderBridge(Context c, int height, OnLoadingComplate onLoadingComplate) {
        this.mContext = c;
        this.height = height;
        this.onLoadingComplate = onLoadingComplate;
    }


    @JavascriptInterface
    public String provideContent() {
        JsonObject jsonObject = new JsonObject();
        jsonObject.addProperty("content", complex.getContent());
        jsonObject.addProperty("summary", complex.getSummary());
        if (complex.getPayment() != null && complex.getPayment().getStatus().equals("1")) {
            jsonObject.addProperty("charge", complex.getPayment().getIntro());
        } else {
            jsonObject.addProperty("charge", "");
        }

        List<UserDetail> topUserList = complex.getPayment().getTop_users();
        jsonObject.addProperty("totalChargeNum", complex.getPayment().getUser_count());
        int size = topUserList.size() > 3 ? 3 : topUserList.size();
        jsonObject.addProperty("chargeUserCount", size);
        for (int i = 0; i < size; i++) {
            jsonObject.addProperty("avatar" + i, topUserList.get(i).getAvatar());
        }

        jsonObject.addProperty("time", String.format(mContext.getString(R.string.post_at), StringUtil.getOppositeTime(complex.getCtime())));
        return jsonObject.toString();
    }

    @JavascriptInterface
    public int provideBodyFontSize() {
        return 16;
    }

    @JavascriptInterface
    public int provideTitleFontSize() {
        return 22;
    }

    @JavascriptInterface
    public int providePaddingTop() {
        return UIUtils.px2dip(height, mContext) + 18;
    }

    @JavascriptInterface
    public int providePaddingBottom() {
        return 48;
    }

    @JavascriptInterface
    public void onDocumentReady() {
        onLoadingComplate.onComplate();
    }

    @JavascriptInterface
    public void onCharge() {
        BaseObserver.getInstance().notifyWatchers(BaseObserver.CHARGE_EVENT);
    }

    @JavascriptInterface
    public void onClickBody() {

    }

    @JavascriptInterface
    public void onChargeRank() {
        BaseObserver.getInstance().notifyWatchers(BaseObserver.CHARGE_RANK);
    }

    public interface OnLoadingComplate {
        void onComplate();
    }


}
